package com.jack.leetcode.eachproblem_2020;

public class 单词规律 {

    public static void main(String[] args) {
        System.out.println(wordPattern("abba", "dog dog dog dog"));
    }

    public static boolean wordPattern(String pattern, String s) {
        if(pattern.length() == 0 && s.length() == 0){
            return true;
        }
        if(pattern.length() == 0 || s.length() == 0){
            return false;
        }
        String[] strings = new String[26];
        String[] s1 = s.split(" ");
        if(pattern.length() != s1.length){
            return false;
        }
        for(int i = 0;i < pattern.length();i++){
            int index = pattern.charAt(i) - 'a';
            if(strings[index] != null && !strings[index].equals(s1[i])){
                return false;
            }
            strings[index] = s1[i];
        }
        for(int i = 0;i < 25;i++){
            for(int j = i + 1;j < 26;j++){
                if(strings[i] != null && strings[j] != null){
                    if(strings[i].equals(strings[j])){
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
